package com.shengqiang.cloud.oauth2.utils;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.type.StandardAnnotationMetadata;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.util.TagUtils;

import java.util.*;

/**
 * Created by Administrator on 2017/12/10.
 */
@Component
public class RedisTool<T> {

    private String clientKey="h:client:%s";

    private String appResourceKey="h:appResource:%s";

    @Autowired
    private Gson gson;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 构建注册客户的hashset的key
     * @param value
     * @return
     */
    public  String generatorClientKey(String value){
        return String.format(clientKey,value);
    }

    /**
     * 构建注册服务端资源保护列表
     * @param value
     * @return
     */
    public String generatorAppResourceKey(String value){
        return String.format(appResourceKey,value);
    }

    /**
     * 插入到hash表中
     * @param hashKey
     * @param valueKey
     * @param value
     * @return
     * @throws Exception
     */
    public boolean saveOpsHash(String hashKey, String valueKey, T value) throws Exception{
        boolean flag=false;
        if(value instanceof String){
            redisTemplate.opsForHash().put(hashKey,value,value);
            flag=true;
        }else{
            String valueStr=gson.toJson(value);
            redisTemplate.opsForHash().put(hashKey,valueKey,valueStr);
            flag=true;
        }

        return flag;
    }

    /**
     * 从hash表中查询数据
     * @param hashKey
     * @param valueKey
     * @param tClass
     * @return
     * @throws Exception
     */
    public T queryOpsHash(String hashKey, String valueKey,Class<T> tClass) throws Exception{
        String value=(String) redisTemplate.opsForHash().get(hashKey,valueKey);
        T data=gson.fromJson(value,tClass);
        return data;
    }

    /**
     * 查询hash表中的值列表
     * @param hkeys
     * @param tClass
     * @return
     */
    public List<T> queryOpsHashList(String hkeys,Class<T> tClass)throws Exception{
        Map hashVals=redisTemplate.opsForHash().entries(hkeys);
        List<T> dataList= Lists.newArrayList();
        Set<String> keys=hashVals.keySet();
        for (String key:keys) {
            String value=hashVals.get(key).toString();
            T data=gson.fromJson(value,tClass);
            dataList.add(data);
        }
        return dataList;
    }

    /**
     * 查询hash表中指定的数据
     * @param hKey
     * @param valueKey
     * @return
     * @throws Exception
     */
    public boolean deleteOpsHash(String hKey,String valueKey) throws Exception{
        long deleteResult=redisTemplate.opsForHash().delete(hKey,valueKey);
        if(deleteResult>0){
            return true;
        }else {
            return false;
        }
    }
}
